<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Login_model {

	public function getindex($id) {

		$list = Db::table('cn_user')
			->alias('cu')
			->join('cn_user_character_relation cucr', 'cu.user_id = cucr.user_id')
			->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			->join('cn_menu_character_relation cmcr', 'cmcr.chara_id = cc.chara_id')
			->join('cn_menu cm', 'cmcr.menu_id = cm.menu_id')
			->where('cn_user.user_id', $id)
			->field('menu_name')
			->select();
		//获取用户历史记录
		$record = Db::table('cn_user_log')
			->alias('cul')
			->where('user_id', $id)
			->field('log_desc')
			->order('create_time desc')
			->limit(9)
			->select();
		$second = Db::table('cn_user_log')
			->alias('cul')
			->where('user_id', $id)
			->field('log_desc,create_time')
			->order('create_time desc')
			->limit(9)
			->select();
		$now = time();
		$s = [];
		for ($i = 0; $i < count($second); $i++) {
			$t = strtotime($second[$i]['create_time']);
			$o[$i]['create_time'] = ($now - $t);
			$time = substr($o[$i]['create_time'], 0, 4);
			if ($time < 60) {
				$s[$i]['create_time'] = $time . '秒前';
			} else if ($time > 60 && $time < 3600) {
				$s[$i]['create_time'] = (floor($time / 60)) . '分钟前';

			} else if ($time > 3600) {
				$s[$i]['create_time'] = (floor($time / 3600)) . '小时前';
			}

		}

		for ($j = 0; $j < count($record); $j++) {
			$amount[$j] = array_merge($record[$j], $s[$j]);
		}

		//获得最新消息
		$mail = Db::table('cn_mail_addressee')
			->alias('cma')
			->join('cn_mail cm', 'cma.mail_id = cm.mail_id')
			->where('add_user_id', $id)
			->order('cm.update_time desc')
			->field('mail_title,cm.mail_id')
			->limit(9)
			->select();
		if (empty($amount)) {
			$amount = null;
		}
		$file_num = Db::table('cn_interfile')
			->where('interfile_status', 3)
			->field('interfile_id')
			->select();
		$file_num = count($file_num);
		//借阅数量暂无
		$borrow_num = 0;
		$user_num = Db::table('cn_user')
			->where('is_delete', 0)
			->field('user_id')
			->select();
		$user_num = count($user_num);
		$info = [
			'node' => $list,
			'amount' => $amount,
			'mail' => $mail,
			'id' => $id,
			'file_num' => $file_num,
			'borrow_num' => $borrow_num,
			'user_num' => $user_num,
		];
		return $info;
	}

}
?>